package com.dabai.app.im;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


public class WebViewActivity extends BaseActivity implements JunHuaWebView.WebViewCallBack {
    @Bind(R.id.webview)
    JunHuaWebView junHuaWebView;

    @Bind(R.id.right_menu_iv)
    ImageView mOptionMenuIv;
    @Bind(R.id.left_menu_iv)
    ImageView mBackMenuIv;
    @Bind(R.id.title_tv)
    TextView mTitle;

//    private Toolbar mToolBar;

    public static final String HTML_URL = "HTML_URL";
    public static final String PARAM_REFRESH_ON_START = "REFRESH_ON_START";
    private String mHtmlUrl = "";
    private boolean mRefreshOnStart = false;
    private ShareParameters mShareParameters;

    WebViewH5Handler mWebViewH5Handler;

    @Override
    public void createActivityView(Bundle savedInstanceState) {
        mWebViewH5Handler = new WebViewH5Handler(this);
        ShareUtils.initShare(this);

        setContentView(R.layout.activity_webview);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        mTitle.setText("正在加载中..");
        mHtmlUrl = "http://10.48.251.91:8087/";
        junHuaWebView.getUrlList().add(mHtmlUrl);
        Log.d("WebViewActivity", mHtmlUrl + "");
        mRefreshOnStart = getIntent().getBooleanExtra(PARAM_REFRESH_ON_START, false);
        junHuaWebView.setWebViewCallBack(this);

        BaseJsObject jsObject = new BaseJsObject(mActivity, junHuaWebView);
        junHuaWebView.addJavaScript(jsObject);
        junHuaWebView.loadUrl(mHtmlUrl);
        mBackMenuIv.setVisibility(View.INVISIBLE);
        mBackMenuIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (junHuaWebView.canGoBack()) {
                    goBack();
                } else {
                    exitBy2Click();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        ShareUtils.stopShare(this);
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mRefreshOnStart) {
            junHuaWebView.reload();
        }
    }

    @Override
    public void onTitleChange(String title) {
        mTitle.setText(title);
    }

    @Override
    public void onCanGoBack(boolean canGoBack) {
        if (canGoBack) {
            mBackMenuIv.setVisibility(View.VISIBLE);
        } else {
            mBackMenuIv.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();
        }
        return false;
    }

    private void goBack() {
        if (junHuaWebView.canGoBack() && junHuaWebView.getUrlList().size() != 1) {
            junHuaWebView.getUrlList().remove(junHuaWebView.getUrl());
            if (junHuaWebView.getUrlList() == null || junHuaWebView.getUrlList().size() == 0) {
//                junHuaWebView.goBack();
                return;
            }
            String showUrl = (String) junHuaWebView.getUrlList().get(junHuaWebView.getUrlList().size() - 1);
////            mTitle.setText(junHuaWebView.getTitleMap().get(showUrl) + "");
////            mHtmlUrl = showUrl;
//
//            if (junHuaWebView.getUrl().equals(showUrl)) {
//                junHuaWebView.reload();
//            } else {
            junHuaWebView.loadUrl(showUrl);
//            }
        } else {
            exitBy2Click();
        }
    }

    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, R.string.app_exit_tips, Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            mActivity.getApplication().sendBroadcast(new Intent("finish"));
        }
    }

    public void onEvent(final WebViewTitleEvent event) {
        UIThread.getInstance().post(new Runnable() {
            @Override
            public void run() {
                mTitle.setText(event.getTitle());
            }
        });
    }

    public void onEvent(JSEvent event) {
        finish();
    }

    public void onEvent(final ShowShareEvent isShow) {
        UIThread.getInstance().post(new Runnable() {
            @Override
            public void run() {
                if (BuildConfig.DEBUG) Log.d("WebViewActivity", "isShow:" + isShow);
                if (isShow.isShow()) {

                    mOptionMenuIv.setVisibility(View.VISIBLE);
                    mOptionMenuIv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (null == mShareParameters) {
                                return;
                            }
                            ShareUtils.showShare(WebViewActivity.this, mShareParameters.getTitle(),
                                    mShareParameters.getShareContent(), mShareParameters.getImageUrl(), mShareParameters.getShareUrlLink());
                        }
                    });
                } else {
                    mOptionMenuIv.setVisibility(View.GONE);
                }
            }
        });
    }

    public void onEvent(ShareParameters shareParameters) {
        mShareParameters = shareParameters;
        if (BuildConfig.DEBUG) Log.d("WebViewActivity", "shareParameters:" + shareParameters);
    }

    public void onEvent(ShowLoadingEvent pEvent) {
        if (pEvent.mShow) {
            showLoading();
        } else {
            hiddenLoading();
        }
    }
}
